package com.shenzhen.horselive.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.shenzhen.horselive.R;
import com.shenzhen.horselive.User;
import com.shenzhen.horselive.adapter.AuctionDetailsAdapter;
import com.shenzhen.horselive.api.ApiFactory;
import com.shenzhen.horselive.base.AppBarActivity;
import com.shenzhen.horselive.beans.AuctionDetailsHeadBean;
import com.shenzhen.horselive.beans.AuctionDetailsRecordList;
import com.shenzhen.horselive.databinding.ActivityAuctionDetailsBinding;
import com.shenzhen.horselive.databinding.HeadAuctionDetailsBinding;
import com.shenzhen.horselive.network.SchedulersCompat;
import com.shenzhen.horselive.presenter.LoadingViewObserver;

/**
 * 城市正在竞拍中
 */
public class MyCityLordAuctioningActivity extends AppBarActivity {

    ActivityAuctionDetailsBinding  binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityAuctionDetailsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setStyleRed();
        setTitle("我的城主");
        setMenuText("竞拍大厅");
        initViews();
        initData();
    }

    private void initData() {
        String areaCode =  getIntent().getStringExtra("id");
        if (areaCode==null)
            return;

        //获取头部信息
        ApiFactory.getApiService().auctionHeadDetails(User.get().getMyInfo().getToken(),areaCode)
                .compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<AuctionDetailsHeadBean>(this) {
                    @Override
                    protected void onNextInActive(AuctionDetailsHeadBean auctionDetailsHeadBean) {
                        if (auctionDetailsHeadBean.isSuccess())
                        {
                            if (headbinding!=null)
                            {
//                                //总收益
                                headbinding.tvValue.setText(auctionDetailsHeadBean.getData().getLastProfit()+"峰值");
                                //起拍价格
                                headbinding.tvValue1.setText(auctionDetailsHeadBean.getData().getStartPrice()+"峰值");
                                //剩余竞拍时间
                                headbinding.tvValue2.setText(auctionDetailsHeadBean.getData().getResidueAuctionTime());
                            }

                        }
                    }
                });

         //获取竞价列表
        ApiFactory.getApiService().auctionDetailsRecordList(User.get().getMyInfo().getToken()
        ,areaCode).compose(SchedulersCompat.applyIoSchedulers())
                .safeSubscribe(new LoadingViewObserver<AuctionDetailsRecordList>(this) {
                    @Override
                    protected void onNextInActive(AuctionDetailsRecordList auctionDetailsRecordList) {
                        if (auctionDetailsRecordList.isSuccess())
                        {
                            headbinding.tvText1.setText(String.format("竞拍记录(%s条)",auctionDetailsRecordList.getData().size()+""));
                            adapter.setNewInstance(auctionDetailsRecordList.getData());
                        }
                    }
                });

    }
    AuctionDetailsAdapter   adapter;
    private void initViews() {
        binding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
          adapter  = new AuctionDetailsAdapter(R.layout.item_auction_details);
        binding.recyclerview.setAdapter(adapter);
        adapter.addHeaderView(getHead());
        binding.btGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MyCityLordAuctioningActivity.this,PayAuctionCityActivity.class));
            }
        });
    }
    HeadAuctionDetailsBinding  headbinding;
    private View getHead() {
        headbinding = HeadAuctionDetailsBinding.inflate(getLayoutInflater());
        return  headbinding.getRoot();
    }


    @Override
    protected void onActionMenuViewClick() {
        super.onActionMenuViewClick();
        startActivity(new Intent(this,AuctionHallActivity.class));
    }

    /**
     *
     * @param context
     * @param id  城市areaCode
     */
    public static void start(Context context,String  id) {
        Intent starter = new Intent(context, MyCityLordAuctioningActivity.class);
        starter.putExtra("id",id);
        context.startActivity(starter);
    }


}